use std::fmt::{
    self, Display,
};

pub type Result<T> = std::result::Result<T, Error>;

/// 自定义异常
#[derive(Debug)]
pub struct Error
{
    message: String,
}

impl Display for Error
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    { write!(f, "Error : {}", self.message) }
}

/// 基础方法
impl Error
{
    pub fn new<M>(message: M) -> Self
        where M: Into<String>
    { Self { message: message.into() } }

    pub fn get_message(&self) -> String
    { self.message.to_string() }
}

/// std::error::Error -> Error
impl<E> From<E> for Error
    where E: std::error::Error
{
    fn from(e: E) -> Self
    {
        Self::new(e.to_string())
    }
}